package cn.maidouya.org.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageLIst;
import cn.maidouya.org.domain.Department;
import cn.maidouya.org.query.DepartmentQuery;
import cn.maidouya.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     *    查询上级部门的数据/department/deptTree
     * @param id
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查找部门",notes = "根据ID查找部门")
    public List<Department> getDeptTree(){
        List<Department> list = departmentService.deptTree();
        return list;
    }
    /**
     *  根据id查询一个部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找部门",notes = "根据ID查找部门")
    public Department finOne(@PathVariable("id") Long id){
        Department departmentOne = departmentService.loadById(id);
        return departmentOne;
    }

    /**
     *  查询所有的部门
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查找全部部门",notes = "查找所有部门信息")
    public List<Department> finAll(){
        return departmentService.loadAll();
    }
    /**
     *   查询分页和总条数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "查找部门分页数据",notes = "分页查找所有部门信息")
    public PageLIst<Department> queryDepartmentPage(@RequestBody DepartmentQuery departmentQuery){
        PageLIst<Department> pageLIst = departmentService.queryData(departmentQuery);
        return pageLIst;
    }

    /**
     *  添加和修改部门
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改部门",notes = "添加和修改部门")
    public AjaxResult addAndUpdata(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("添加失败");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "根据id删除部门")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *   批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除部门",notes = "根据id批量删除部门")
    public AjaxResult batDel(@RequestBody Long[] ids){
        System.err.println(ids);
        try {
            departmentService.batDel(ids);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("批量删除失败");
        }

    }
}